package com.mds.organ.web.screens.organ;

import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.gui.UiComponents;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.FileDescriptorResource;
import com.haulmont.cuba.gui.components.Image;
import com.haulmont.cuba.gui.components.TreeTable;
import com.haulmont.cuba.gui.screen.*;
import com.mds.organ.entity.Organ;

import javax.inject.Inject;

@UiController("mdsorgan_Organ.browse")
@UiDescriptor("organ-browse.xml")
@LookupComponent("organsTable")
@LoadDataBeforeShow
public class OrganBrowse extends StandardLookup<Organ> {
    @Inject
    TreeTable<Organ> organsTable;

    @Inject
    UiComponents uiComponents;

    @Subscribe
    protected void onInit(InitEvent event){
        organsTable.addGeneratedColumn("photo",1,this::renderPhoto);
    }

    private Component renderPhoto(Organ organ){
        FileDescriptor fd=organ.getPhoto();
        if(fd == null){
            return null;
        }
        Image image=formatPhoto();
        image.setSource(FileDescriptorResource.class)
                .setFileDescriptor(fd);

        return image;
    }

    private Image formatPhoto(){
        Image img=uiComponents.create(Image.class);
        img.setScaleMode(Image.ScaleMode.CONTAIN);
        img.setHeight("40");
        img.setWidth("40");
        img.setStyleName("avatar-icon-small");
        return img;
    }
}